package com.Lyf.user.consumer;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

import static com.Lyf.user.config.RabbitMQConstant.*;


@Component
@Slf4j
public class Consumer {

    //需要添加下面的注解(写上要监听队列的名称)
    //当有消息送到队列中时,@RabbitListener注解能够自动获取到消息


    //不需要返回值,否则会出现诡异报错
    //直接使用String去接收消息
    @RabbitListener(queues = DEAD_LETTER_QUEUE_A )
    public void readMsg(String message,Channel channel, Message msg)
    {

        System.out.println("从死信队列读取消息-----"+message);
        try {
            //手动ack
            channel.basicAck(msg.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    //手动ack需要三个参数
    @RabbitListener(queues = "dead.letter.queue.b")
    public void readMsgACK(String message, Channel channel, Message msg)
    {
        log.info("read msg from MQ  , the message is:"+message);
        try {
            //手动ack
            channel.basicAck(msg.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
